import 'package:flutter/cupertino.dart';
import 'package:t3_data_service/biz_data_source/map/address/city_pick/city_pick.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:t3_foundation/t3_foundation.dart';
import 'package:t3_map/configs/poi_search_base_config.dart';
import 'package:t3_map/poi_search/view_model/poi_search_cell_view_model.dart';
import 'package:uuid/uuid.dart';

import '../../page/poi_search_channel.dart';

class InputItem {
  final String id = const Uuid().v1();

  T3POILocation? location;
  T3POIType poiType = T3POIType.start;
  String? keyword;
  POISearchCellViewModel? poi;

  /// dot定制， 互斥
  Color? dotColor;
  String? dotImageName;
  FocusNode focusNode = FocusNode();

  String placeholder = "";

  bool isReorderButtonHidden = false;
  bool isDeleteButtonHidden = false;
  bool isAddMidWayButtonHidden = false;
  bool isFirstResponder = false;

  EdgeInsets margin = EdgeInsets.zero;

  InputItem({this.location, this.poi}) {
    var adCode = poi?.poi.adCode ?? location?.adCode;
    var coordinate = poi?.poi.location ?? location?.coordinate;
    if (adCode.isNullOrEmpty()) {
      var result = POISearchChannel.patchCondition(coordinate);
      result.then((value) {
        if (value.item1.isNotNullAndEmpty()) {
          location?.adCode = value.item1!;
        }
        var city = location?.cityName;
        if (value.item2.isNotNullAndEmpty() && city.isNullOrEmpty()) {
          location?.cityName = value.item2!;
        }
      });
    }
  }

  String displayText() {
    var displayName = poi?.multiInputDisplayName ?? '';
    if (displayName.isNotEmpty) {
      return displayName;
    }
    /// 图选回来的点
    if (poi?.mapSelectedData != null) {
      var name = poi?.mapSelectedData?['addressTitle'];
      if (name != null && name is String) {
        return name;
      }
    }
    return keyword ?? "";
  }

  String cityName() {
    var name = poi?.multiInputCityName ?? location?.cityName ?? "";
    if (name.endsWith('市')) {
      name = name.substring(0, name.length - 1);
    }
    if (name.isNotEmpty) {
      return name;
    }
    /// 图选回来的点
    if (poi?.mapSelectedData != null) {
      var city = poi?.mapSelectedData?['city'];
      if (city != null && city is String) {
        if (city.endsWith('市')) {
          city = city.substring(0, city.length - 1);
          return city;
        } else {
          return city;
        }
      }
    }
    return name;
  }

  String? adCode() {
    return poi?.poi.adCode ?? location?.adCode;
  }

  LatLng? coordinate() {
    return poi?.poi.location ?? location?.coordinate;
  }

  String? getKeyword() {
    return poi?.poiName ?? keyword;
  }

  changePoi(POISearchCellViewModel poi) {
    this.poi = poi;
    keyword = null;
  }

  changeKeyword(String text) {
    keyword = text;
    poi = null;
  }

  changeCity(HostModel model) {
    location = model.toLocation();
    poi = null;
  }

  @override
  String toString() {
    return "${cityName()}-$id";
  }

  T3SearchCondition generateCondition(POISearchBaseConfig config) {
    final condition = T3SearchCondition(
        adCode: adCode() ?? '',
        cityName: cityName(),
        coordinate: coordinate() ?? const LatLng(0, 0));
    condition.poiType = poiType;
    condition.supplier = config.mapType;
    if (poi == null) {
      condition.keyword = keyword;
    }
    return condition;
  }

  /// 生成城市选择需要的数据结构
  HostModel generateHostModel(){
    if(null == poi) {
      return HostModel(city: "",adCode: "",amapCityCode: "");
    }
    var city = "";
    var adCode = "";
    var amapCityCode = "";
    city = poi?.poi.city ?? "";
    adCode = poi?.poi.adCode ?? "";
    amapCityCode = poi?.poi.cityCode ?? "";
    // 先从poi从获取数据
    if(city.isNotEmpty && adCode.isNotEmpty) {
      return HostModel(city: city,adCode: adCode,amapCityCode: amapCityCode);
      // 再从图选数据中获取
    } else if(poi?.mapSelectedData != null){
      var citySelect = poi?.mapSelectedData?['city'];
      if (citySelect != null && citySelect is String) {
        if (citySelect.endsWith('市')) {
          city = citySelect.substring(0, citySelect.length - 1);
        }
      }
      var adCodeSelect = poi?.mapSelectedData?['adCode'];
      if (adCodeSelect != null && adCodeSelect is String) {
        adCode = adCodeSelect;
      }

      var cityCodeSelect = poi?.mapSelectedData?['cityCode'];
      if (cityCodeSelect != null && cityCodeSelect is String) {
        amapCityCode = cityCodeSelect;
      }
      return HostModel(city: city,adCode: adCode,amapCityCode: amapCityCode);
    } else {
      return HostModel(city: "",adCode: "",amapCityCode: "");
    }
  }
}

extension _Convert on HostModel {
  T3POILocation toLocation() {
    return T3POILocation(
        adCode: adCode ?? '',
        cityName: city ?? '',
        coordinate: LatLng(latitude ?? 0, longitude ?? 0)
    );
  }
}
